package com.wisemoresystem.seal.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.page.Page;
import org.ne.framework.data.task.Task;
import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wisemoresystem.seal.base.service.BaseService;
import com.wisemoresystem.seal.web.WebTools;

@Service
public class UseSearchErrorService extends BaseService {

	@Autowired
	private BankService bService;

	@Task(code = "352", tip = "异常列表(@cnt)")
	public long selectUseSealErrorList() {

		String sql = "select count(*) cnt from t_seal_use_apply t "
				+ " join t_use_search_log tl on tl.apply_id=t._id and tl.search_result!='正常' "
				+ " and (tl.search_type='self search' or tl.search_type='city search' or tl.search_type='pro search') "
				+ " where t.status=0 and t.bank_id=? and t.seal_status='已盖章' "
				+ " and (t.verify_status='已自审' or t.verify_status='市级审核' or t.verify_status='省级审核') ";

		Long cnt = super.getTaskCnt(sql, WebTools.getCurrentBankId());
		
		String sql1 = "select count(*) cnt from t_seal_use_apply t "
				+ " join t_use_search_log tl on tl.apply_id=t._id and tl.search_result!='正常' "
				+ " and (tl.search_type='self search' or tl.search_type='city search' or tl.search_type='pro search') "
				+ " join t_use_search_error te on t._id=te.apply_id and te.e_time is not null "
				+ " where t.status=0 and t.bank_id=? and t.seal_status='已盖章' "
				+ " and (t.verify_status='已自审' or t.verify_status='市级审核' or t.verify_status='省级审核') ";
		Long cnt1 = super.getTaskCnt(sql1, WebTools.getCurrentBankId());
		
		return cnt-cnt1;
	}
	
	// 异常处理列表
	public List<Map<String, Object>> selectUseSealErrorList(String srh_reason,
			String srh_name, Page page) {

		String sql = "select t.*,tu.uname,ts.name,ts.true_code,tl.search_result,te.e_time,te.e_content from t_seal_use_apply t "
				+ " left join t_user tu on t.apply_user=tu._id "
				+ " left join t_seal ts on t.seal_id=ts._id "
				+ " join t_use_search_log tl on tl.apply_id=t._id and tl.search_result!='正常' "
				+ " and (tl.search_type='self search' or tl.search_type='city search' or tl.search_type='pro search') "
				+ " left join t_use_search_error te on t._id=te.apply_id "
				+ " where t.status=0 and t.bank_id=? and t.seal_status='已盖章' "
				+ " and (t.verify_status='已自审' or t.verify_status='市级审核' or t.verify_status='省级审核') and t.reason like ? "
				+ " and (ts.name like ? or ts.true_code like ?) order by t.created desc ";

		List<String> params = new ArrayList<String>();
		params.add(WebTools.getCurrentBankId());
		if (ChkTools.isNull(srh_reason)) {
			sql = sql.replace("and t.reason like ?", "");
		} else {
			params.add("%" + srh_reason + "%");
		}
		if (ChkTools.isNull(srh_name)) {
			sql = sql
					.replace("and (ts.name like ? or ts.true_code like ?)", "");
		} else {
			params.add("%" + srh_name + "%");
			params.add("%" + srh_name + "%");
		}

		List<Map<String, Object>> list = jc.queryForList(sql, params.toArray(),
				page);
		return list;
	}

	// 异常列表
	public List<Map<String, Object>> selectUseSealErrorManList(
			String srh_reason, String srh_name, Page page) {

		String sql = "select t.*,tu.uname,ts.name,ts.true_code,te.e_content from t_seal_use_apply t "
				+ " left join t_user tu on t.apply_user=tu._id "
				+ " left join t_seal ts on t.seal_id=ts._id "
				+ " join t_use_search_log tl on tl.apply_id=t._id and tl.search_result!='正常' "
				+ " and (tl.search_type='self search' or tl.search_type='city search' or tl.search_type='pro search') "
				+ " left join t_use_search_error te on t._id=te.apply_id and te.e_time is not null"
				+ " where t.status=0 and t.bank_id=? and t.seal_status='已盖章' "
				+ " and (t.verify_status='已自审' or t.verify_status='市级审核' or t.verify_status='省级审核') and t.reason like ? "
				+ " and (ts.name like ? or ts.true_code like ?) order by t.created desc ";

		List<String> params = new ArrayList<String>();
		params.add(WebTools.getCurrentBankId());
		if (ChkTools.isNull(srh_reason)) {
			sql = sql.replace("and t.reason like ?", "");
		} else {
			params.add("%" + srh_reason + "%");
		}
		if (ChkTools.isNull(srh_name)) {
			sql = sql
					.replace("and (ts.name like ? or ts.true_code like ?)", "");
		} else {
			params.add("%" + srh_name + "%");
			params.add("%" + srh_name + "%");
		}

		List<Map<String, Object>> list = jc.queryForList(sql, params.toArray(),
				page);

		return list;
	}
}
